﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Services;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for UserListView.xaml
    /// </summary>
    public partial class UserListView : UserControl, INavigationAware
    {
        private UserListViewModel Model;
        private readonly IUnityContainer _container;
        private readonly IUserController _userController;
        private readonly IUserService _userService;
        private readonly IUserFactory _userFactory;

        public UserListView(IUnityContainer container, IUserController userController, IUserService userService, IUserFactory userFactory)
        {
            InitializeComponent();

            _container = container;
            _userController = userController;
            _userService = userService;
            _userFactory = userFactory;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            await Task.Run(() => Model = _userService.GetListViewModel());
            //new UserListViewModel();
           // Model.Users = _userFactory.GetList();

            this.DataContext = Model;
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            _userController.NavigateToItemView();
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid grid = sender as DataGrid;

            if (grid.SelectedItem != null)
            {
                var user = grid.SelectedItem as UserModel;
                _userController.NavigateToItemView(user.UserGuid);
            }
        }
    }
}
